package top.qitun.controller;

import java.util.Date;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import top.qitun.bizz.cart.cartevent.entity.CartEvent;
import top.qitun.bizz.cart.cartevent.entity.CartEventBO;
import top.qitun.bizz.cart.cartevent.service.CartEventService;
import top.qitun.modal.request.CartEventCreateRequest;
import top.qitun.model.response.ObjectDataResponse;
import top.qitun.util.OrikaMapper;

/**
 * 店铺创建类
 * @author Cloudwei
 *
 */
@RestController
@RequestMapping(value = "/v1")
public class CartController {
	Logger logger = LoggerFactory.getLogger(getClass());
	
    @Autowired
    private CartEventService cartEventService;
    
    @ApiOperation(value = "增加购物车点菜事件", notes = "增加购物车点菜事件")
    @RequestMapping(path = "/events", method = RequestMethod.POST)
    public ObjectDataResponse<CartEventBO> addCartEvent(@RequestBody CartEvent cartEvent) throws Exception {
    	String desc = "增加购物车点菜事件";
    	try{
    		Date now = new Date();
    		cartEvent.setCreateTime(now);
    		cartEvent.setLastUpdateTime(now);
    		cartEventService.add(cartEvent);
    		return new ObjectDataResponse<>(0,desc+"成功");
    	}catch(Exception e){
    		logger.error(desc+"失败",e);
    		return new ObjectDataResponse<>(-1,desc+"失败.请联系客服"+e.getMessage());
    	}
    }

	@ApiOperation(value = "下单,生成订单", notes = "下单,生成订单.修改购物车状态")
	@RequestMapping(path = "/checkout/table/{tableid}", method = RequestMethod.POST)
	public ObjectDataResponse<CartEventBO> checkoutCart(
			@ApiParam(example = "2", defaultValue = "2", value = "台号Id") @PathVariable(value = "tableid") Integer tableId)
			throws Exception {
		String desc = "下单,生成订单";
		try {
			cartEventService.checkout(tableId);
			return new ObjectDataResponse<>(1, desc + "成功");
		} catch (Exception e) {
			logger.error(desc + "失败", e);
			return new ObjectDataResponse<>(-1, desc + "失败,请联系管理员");
		}
	}

}
